package com.xisoft.base.core.listener;

import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;


public class MyApplicationRunnerlistener implements SpringApplicationRunListener
{
    public MyApplicationRunnerlistener( )
    {
    }

    public MyApplicationRunnerlistener(SpringApplication application, String[] args)
    {
        // 要加构造函数，不加会报错
        // Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface
        // org.springframework.boot.SpringApplicationRunListener :
        // pres.bc.stu.web.listener.MyApplicationRunnerlistener

        // java.lang.NoSuchMethodException:
        // pres.bc.stu.web.listener.MyApplicationRunnerlistener.<init>(org.springframework.boot.SpringApplication,
        // [Ljava.lang.String;)

    }

    // @Override public void started(ConfigurableApplicationContext context, Duration timeTaken)
    // {
    //     System.err.println("🐞🀙👉MyApplicationRunnerlistener....started....👈");
    //     SpringApplicationRunListener.super.started(context, timeTaken);
    // }

    @Override
    public void starting(ConfigurableBootstrapContext bootstrapContext)
    {
        System.err.println("🐞🀙👉MyApplicationRunnerlistener....starting....👈");
        System.err.println("1、IOC容器还没有创建调用此方法...");
        SpringApplicationRunListener.super.starting(bootstrapContext);
    }

    @Override
    public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment)
    {
        System.err.println("🐞🀚👉MyApplicationRunnerlistener....environmentPrepared....环境准备完成👈"
                                   + environment.getSystemProperties().get("os.name"));
        System.err.println("2、基础环境完成调用此方法...");

        SpringApplicationRunListener.super.environmentPrepared(bootstrapContext, environment);
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context)
    {
        System.err.println("🐞🀛👉MyApplicationRunnerlistener....contextPrepared....IOC容器准备好了...👈");
        System.err.println("3、context准备完成调用此方法...");
        SpringApplicationRunListener.super.contextPrepared(context);
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context)
    {
        System.err.println("🐞🀜👉MyApplicationRunnerlistener....contextLoaded....IOC容器加载完成...👈");
        System.err.println("4、context运行完成调用此方法...");

        SpringApplicationRunListener.super.contextLoaded(context);
    }

    @Override
    public void running(ConfigurableApplicationContext context)
    {
        System.err.println("🐞🀝👉MyApplicationRunnerlistener....running....👈" + context);
        System.err.println("5、web应用启动完成调用此方法...");
        // System.err.println(0 / 0);
        SpringApplicationRunListener.super.running(context);
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception)
    {
        System.err.println("❌💥❣👉MyApplicationRunnerlistener....failed....SpringBoot启动失败：👈" + exception);
        System.err.println("5、web应用启动失败调用此方法...");

        SpringApplicationRunListener.super.failed(context, exception);
    }

}
